package com.jxwifi.cloud.quickclean.address;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.basicactivity.BasicActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.widget.LoadDialog;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.ClientAddressListAdapter;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.bean.TextBean;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/6/14.
 */

public class AddressListActivity extends ClientBasicActivity implements AddressListView {

    @Bind(R.id.rv_address_list)
    RecyclerView mRvAddressList;

    private View notDataView;
    private LoadDialog mLoadDialog;
    private AddressListPresenter mPresenter;

    private int mSize;//集合大小

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        ButterKnife.bind(this);
        openEventBus();
        mLoadDialog = new LoadDialog(mBaseContext);
        mPresenter=new AddressListPresenter(mBaseContext,this);
    }

    public void initView() {
        mLoadDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.getdata();
                mLoadDialog.dismiss();
            }
        }, 1000);
    }

    @OnClick({R.id.rel_img_back_pressed, R.id.rel_add_address})
    void clickCK(View v) {
        switch (v.getId()) {
            //后退
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            //添加地址
            case R.id.rel_add_address:
                Intent intent=new Intent();
                intent.setClass(mBaseContext,AddressActivity.class);
                intent.putExtra("addorcompile","add");
                intent.putExtra("index_size",mSize);
                startActivity(intent);
                break;
        }
    }

    @Override
    public RecyclerView getRView() {
        return mRvAddressList;
    }

    @Override
    public View getViews() {
        notDataView = getLayoutInflater().inflate(R.layout.null_layout, (ViewGroup) mRvAddressList.getParent(), false);
        return notDataView;
    }

    @Override
    public void onDefaultaddress() {
        finish();
    }

    @Override
    public void onDeleteaddress() {
        initView();
    }

    @Override
    public void getListSize(int indexsize) {
        mSize=indexsize;
    }

    @Override
    public void onDefaults(String StrDefaults) {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.onDefault(StrDefaults);
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public void onDelete(String StrDefaults) {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.onDelete(StrDefaults);
        }
    }

    @Override
    public void ondata() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getdata();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }
}
